package cn.hloger.spider.demo.newsDemo.handler.news;

import cn.hloger.spider.factory.NoRedirectSimpleClientHttpRequestFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * TODO
 * 表情图
 * 这边表情接口会进行重定向302, 所以一般的方法抓不到表情的真实地址
 * 所以目的是抓到302返回的结果, 里面肯定有标签的真实地址
 * 使用特殊的new NoRedirectSimpleClientHttpRequestFactory() 设置重定向为false 就可以了
 *
 * @author huanglong
 * @Description TODO
 * @data 2022/9/18
 * @see NoRedirectSimpleClientHttpRequestFactory
 */
public class EmojiHandler {

    private static String baseUrl = "https://api.isoyu.com/";
    private static String phpUrl = "ARU_GIF_S.php";
    private static final RestTemplate REST_TEMPLATE = new RestTemplate(new NoRedirectSimpleClientHttpRequestFactory());

    public static String getEmojiUrl() {
        ResponseEntity<String> newlist = REST_TEMPLATE.getForEntity(baseUrl + phpUrl, String.class);
        List<String> location = newlist.getHeaders().get("Location");
        if (CollectionUtils.isEmpty(location)) {
            return "";
        }
        String picPath = location.get(0);
        return baseUrl + picPath;
    }

}
